<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancel">
        <rx-layout>
            <div slot="center">
                <a-form style="height:100%;">
                    <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="$comi18('rxDialog.name', '标题')">
                        <a-input v-model="title" ></a-input>
                    </a-form-item>
                    <a-form-item :label-col="labelCol1" :wrapper-col="wrapperCol1" :label="i18n('multipleChoice','多选')">
                        <a-switch size="small" v-model="settings.multiSelect"/>
                    </a-form-item>
                    <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('form.dialog','对话框')">
                        <rx-input-button ref="dialog" @click="selectDialog" v-model="dialog" />
                    </a-form-item>
                    <a-row>
                        <a-col :span="12">
                            <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" :label="i18n('form.text','文本')">
                                <a-select :options="sqlFields" v-model="textField" :getPopupContainer="(p)=>p.parentNode"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" :label="$comi18('listDef.val', '值')">
                                <a-select :options="sqlFields" v-model="valueField" :getPopupContainer="(p)=>p.parentNode"/>
                            </a-form-item>
                        </a-col>
                    </a-row>
                    <a-form-item :labelCol="labelCol1" :wrapperCol="wrapperCol1" :label="i18n('defValue','默认值')">
                        <a-input v-model="value"></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </rx-layout>
    </rx-dialog>
</template>

<script>
import filter from "./filter";
import {Dialog} from "@lowcode/jpaas-common-lib";
import FormBoListApi from "@/api/form/core/formBoList";

export default {
    name: "dialog-filter",
    mixins:[filter],
    data() {
        return {
            dialog:"",
            textField:"",
            valueField:"",
            sqlFields:[]
        }
    },
    created() {

    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"codeGenSetting",key);
        },
        //初始化配置
        initSetting() {
            if(this.row.settings){
                this.settings =this.row.settings;
                if(this.settings.dialog) {
                    this.$set(this,"dialog",this.settings.dialog);
                    this.$set(this,"textField",this.settings.textField);
                    this.$set(this,"valueField",this.settings.valueField);
                    this.loadFields(JSON.parse(this.settings.dialog).value);
                }
            }else {
                this.settings = {
                    dialog:"",
                    valueField:"",
                    textField:"",
                    multiSelect:false
                }
            }
        },
        //获取配置
        getSetting(){
            this.$set(this.settings,"dialog",this.dialog);
            this.$set(this.settings,"valueField",this.valueField);
            this.$set(this.settings,"textField",this.textField);
            return this.settings;
        },
        selectDialog(vm){
            Dialog.openFormBoListDialog({
                curVm: this, widthHeight: ['800px', '600px'],data:{isDialog:'YES'}
            }, function (self, data) {
                var item=data[0];
                self.loadFields(item.key);
                vm.setVal(item.key,item.name);
            })
        },
        loadFields(key){
            FormBoListApi.getByKey(key).then(res=>{
                var data=res.data;
                var fieldsStr=data.fieldsJson;
                if(!fieldsStr) return;
                var fileds=JSON.parse(fieldsStr);
                var ary=[];
                fileds.map(function(item){
                    if(item.isReturn) {
                        ary.push({
                            value: item.field,
                            label: item.header
                        })
                    }
                })
                this.sqlFields=ary;
            })
        },
    },
    watch: {
    }
}
</script>

<style scoped>
</style>